package com.leetcode.offer.chapter4;

import com.leetcode.basic.TreeNode;

import java.util.*;

/**
 * @author Dennis Li
 * @date 2020/7/13 20:41
 */
public class LevelOrder_32 {

    public int[] levelOrder(TreeNode root) {
        if(root == null){
            return new int[0];
        }
        Queue<TreeNode> queue = new LinkedList<>();
        List<Integer> nums = new ArrayList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode node = queue.remove();
            nums.add(node.val);
            if(node.left != null){
                queue.add(node.left);
            }
            if(node.right != null){
                queue.add(node.right);
            }
        }
        int[] res = new int[nums.size()];
        for(int i = 0; i < res.length; i++){
            res[i] = nums.get(i);
        }
        return res;
    }

}
